Import Statements
In [1]:
import tkinter as tk
from tkinter import ttk
from analytical import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
Define Convinence Classes
In [2]:
SCT = Scatterer()
ICONST = 1
class SphereRow():
def __init__(self,frame):
#Store
self.EXISTS = False
i = ICONST
self.i= ICONST
self.frame = frame
#Declare String Variables
self.x_var = tk.DoubleVar()
self.y_var = tk.DoubleVar()
self.z_var = tk.DoubleVar()
self.rho_var = tk.DoubleVar()
self.radius_var = tk.DoubleVar()
#X,Y,Z
self.x = tk.Label(self.frame, text="X:", font=LARGE_FONT)
self.x.grid(column=2, row=i)
self.x_val = tk.Entry(self.frame, textvariable=self.x_var)
self.x_val.grid(column=3, row=i)
self.y = tk.Label(self.frame, text="Y:", font=LARGE_FONT)
self.y.grid(column=4, row=i)
self.y_val = tk.Entry(self.frame, textvariable=self.y_var)
self.y_val.grid(column=5, row=i)
self.z = tk.Label(self.frame, text="Z:", font=LARGE_FONT)
self.z.grid(column=6, row=i)
self.z_val = tk.Entry(self.frame, textvariable=self.z_var)
self.z_val.grid(column=7, row =i)
#Rho
self.rho = tk.Label(self.frame, text="Rho:", font=LARGE_FONT)
self.rho.grid(column=8, row=i)
self.rho_val = tk.Entry(self.frame, textvariable=self.rho_var)
self.rho_val.grid(column=9, row =i)
#Radius
self.radius= tk.Label(self.frame, text="Radius:", font=LARGE_FONT)
self.radius.grid(column=10, row=i)
self.radius_val = tk.Entry(self.frame, textvariable=self.radius_var)
self.radius_val.grid(column=11, row =i)
#Declare Buttons
self.Confirm = tk.Button(self.frame, text= "Confirm", command =lambda: AddSphere(self))
self.Confirm.grid(column=12, row=i)
self.Delete = tk.Button(self.frame,text="Delete", command =lambda: Delete(self))
self.Delete.grid(column=13, row=i)
self.WidgetList = [self.x,self.x_val,self.y,self.y_val,self.z,self.z_val,self.rho,
self.rho_val,self.radius,self.radius_val,self.Confirm,self.Delete]
def AddSphere(self):
#store every value so entry.get() is called only once per entry feild
Rho,Radius = float(self.rho_var.get()), float(self.radius_var.get())
X,Y,Z = float(self.x_var.get()),float(self.y_var.get()),float(self.z_var.get())
#if the shape is contained in the scatterer
#update each value
if self.EXISTS == True:
SCT.shapes[self.i-1].x = X
SCT.shapes[self.i-1].y = Y
SCT.shapes[self.i-1].z = Z
SCT.shapes[self.i-1].contrast = Rho
SCT.shapes[self.i-1].r = Radius
#if the shape isn't contained, declare a new one
else:
mono_sphere(SCT, Rho,Radius,X,Y,Z)
self.EXISTS = True
def Delete(self):
if self.EXISTS==True:
self.EXISTS = False
SCT.shapes[self.i].remove()
for Widget in self.WidgetList:
Widget.grid_remove()
global ICONST
ICONST -=1
In [2]:
Open up a frame and define function
In [ ]:
#BROKEN
LARGE_FONT=("Verdanna", 12)
class BasicApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.pack(side="top",fill="both", expand= True)
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames = {}
for F in (HOME, SCATTERER):
frame = F(container, self)
self.frames[F]=frame
frame.grid(row=0,column=0,sticky="nsew")
self.show_frame(HOME)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class HOME(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self,text="Welcome to Pysaxs", font=LARGE_FONT)
label.grid(column=1,row=1)
button1 = tk.Button(self, text="NEW",
command=lambda: controller.show_frame(SCATTERER))
button1.grid(column=1, row=2)
class SCATTERER(tk.Frame):
def __init__(self,parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self,text="Scatterer Instance", font=LARGE_FONT)
label.grid(column=1,row=1)
self.rows=[]
AddSphere = tk.Button(self, text="Add Sphere",
command = lambda: self.AddSphereRow() )
AddSphere.grid(column=1,row=2)
IQPlot = tk.Button(self, text="Plot",
command = lambda: self.Plot())
IQPlot.grid(column=1,row=3)
self.i = 0 # i is the maximimum row number
def AddSphereRow(self):
global ICONST
ICONST+=1
temp=SphereRow(self)
self.rows.append(temp)
def Plot(self):
gen = SCT.genIQ()
IQ = np.asarray(list(gen))
print(IQ)
TOPLEVEL = tk.Toplevel()
TOPLEVEL.title("Plot")
#plotting
f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
a.plot(Q_RANGE,IQ)
canvas = FigureCanvasTkAgg(f, TOPLEVEL)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2TkAgg(canvas, TOPLEVEL)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
root = BasicApp()
root.title("Pysaxs")
root.mainloop()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
print("BYE BYE")
self.x.destroy()
self.x_val.destory()
self.y.destroy()
self.y_val.destroy()
self.z.destroy()
self.z_val.destroy()
self.rho.destroy()
self.rho_val.destroy()
self.radius.destroy()
self.radius_val.destroy()
self.Confirm.destroy()
self.Delete.destroy()
if self.Scatterer.shapes[self.i]:
self.Scatterer.shapes.remove(self.Scatterer.shapes[self.i])